package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.Order;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;




    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public BaseResult addOrder(@RequestParam("getid") int getid,
                               @RequestParam("backid") int backid,
                               @RequestParam("cid") int cid,
                               @RequestParam("oprice") double oprice, HttpSession session){
        BaseResult baseResult = new BaseResult();
        Order order =new Order();
        User user_ = (User) session.getAttribute("user");
        if(user_==null){
            baseResult.setCode(10001);
            return baseResult;
        }
        int uid = user_.getId();
        order.setBackid(backid);
        order.setUid(uid);
        order.setOprice(oprice);
        order.setCid(cid);
        order.setGetid(getid);
        order.setStatus("已预订");

        return orderService.addOrder(order);
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public BaseResult findAll(@RequestParam("page") int page,HttpSession session){
        BaseResult baseResult = orderService.viewMyOrder(session);
        System.out.println("br1:"+baseResult);
        List<Order> rows =(List<Order>) baseResult.getInfo();
        System.out.println("rows::"+rows);
        PageHelper.startPage(page,5);
        PageInfo<Order> pageInfo = new PageInfo<>(rows);
        baseResult.setInfo(pageInfo.getList());
        System.out.println("pageInfo.getList():"+pageInfo.getList());
        baseResult.setTotal(pageInfo.getTotal());
        System.out.println("pageInfo.getTotal():"+pageInfo.getTotal());
        System.out.println("br2:"+baseResult);
        return baseResult;
    }

    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public BaseResult delMyOrder(@RequestParam("oid") int oid){
        return orderService.delMyOrder(oid);
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public BaseResult findAll(@RequestParam("page") int page,@RequestParam("limit") int limit){

       return orderService.findAll();
    }

}
